import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;


public class MessageMain {
    private static final String TAG = "MessageMain";

    public static void main(String[] args) {
        Looper.prepareMainThread();
        Log.setEnable(true);
        HandlerThread handlerThread = new HandlerThread();
        handlerThread.start();
        Handler handler = new Handler(handlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d("TAG", "testSendEmptyMessageDelayed: " + msg.what);
                System.exit(0);
            }
        };

        Message msg = handler.obtainMessage();
        msg.what = 100;
        Log.d("TAG", "testSendEmptyMessageDelayed: ");
        handler.sendEmptyMessageDelayed(msg.what, 400);


//        Log.d(TAG, "1、创建Runnable的线程id = " + Thread.currentThread().getId());
//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                Log.d(TAG, "1、执行Runnable的线程id = " + Thread.currentThread().getId());
//            }
//        });
//
//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                Log.d(TAG, "2、创建Runnable的线程id = " + Thread.currentThread().getId());
//
//                Handler main = new Handler(Looper.getMainLooper());
//                main.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        Log.d(TAG, "2、执行Runnable的线程id = " + Thread.currentThread().getId());
//                        System.exit(0);
//                    }
//                });
//            }
//        });

        Log.d(TAG, "main: 1:300");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "run: 1:300");
            }
        }, 300);

        Log.d(TAG, "main: 2:200");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "run: 2:200");
            }
        }, 200);

        Log.d(TAG, "main: 3:100");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "run: 3:100");
            }
        }, 100);

        Looper.loop();
    }

    public static class HandlerThread extends Thread {
        private Looper mLooper;

        @Override
        public void run() {
            super.run();
            Looper.prepare();
            synchronized (this) {
                mLooper = Looper.myLooper();
                notifyAll();
            }
            Looper.loop();
        }

        public Looper getLooper() {
            if (!isAlive()) {
                return null;
            }
            synchronized (this) {
                while (isAlive() && mLooper == null) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }
            }
            return mLooper;
        }
    }
}
